package school;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.xml.sax.InputSource;

/**
 * Class used to build a scene from a given sdl file. Implements the
 * ParserHandler interface (these methods need to be filled in by you).
 * 
 * Note that this class keeps the absolute path to the directory where the sdl
 * file was found. If you put your textures in the same directory, you can use
 * this path to construct an absolute file name for each texture. You will
 * probably need absolute file names when loading the texture.
 */
public class SceneBuilder implements ParserHandler {

	// the scene being build
	private Scene scene = null;

	
	// the path to the xml directory
	// this path can be used to put in front of the texture file name
	// to load the textures
	private String path = null;

	public String getPath() {
	
		return path;
	}

	public Scene loadScene(String file) throws FileNotFoundException {
		return loadScene(new File(file));
	}

	/**
	 * Load a scene.
	 * 
	 * @param filename
	 *            The name of the file that contains the scene.
	 * @return The scene, or null if something went wrong.
	 * @throws FileNotFoundException
	 *             The file could not be found.
	 */
	public Scene loadScene(File filename) throws FileNotFoundException {
		// create file and file input stream
		File file = filename;
		FileInputStream fileInputStream = new FileInputStream(file);

		// set the system id so that the dtd can be a relative path
		// the first 2 lines of your sdl file should always be
		// <?xml version='1.0' encoding='utf-8'?>
		// <!DOCTYPE Sdl SYSTEM "sdl.dtd">
		// and sdl.dtd should be in the same directory as the dtd
		// if you experience dtd problems, commend the doctype declaration
		// <!-- <!DOCTYPE Sdl SYSTEM "sdl.dtd"> -->
		// and disable validation (see further)
		// although this is in general not a good idea

		InputSource inputSource = new InputSource(fileInputStream);
		//XXX:String parentPath = file.getParentFile().getAbsolutePath() + "/";
		path = file.getParentFile().getAbsolutePath() + "/";
		inputSource.setSystemId("file:///"
				+ file.getParentFile().getAbsolutePath() + "/");

		// create the new scene
		scene = new Scene();

		// create the parser and parse the input file
		Parser parser = new Parser();
		parser.setHandler(this);

		// if the output bothers you, set echo to false
		// also, if loading a large file (with lots of triangles), set echo to
		// false
		// you should leave validate to true
		// if the docuement is not validated, the parser will not detect syntax
		// errors
		if (parser.parse(inputSource, /* validate */true, /* echo */true) == false) {
			scene = null;
		}

		// return the scene
		return scene;
	}

	/*
	 * (non-Javadoc) ParserHandler callbacks
	 */

	public void startSdl() throws Exception {
	}

	public void endSdl() throws Exception {
	}

	public void startCameras() throws Exception {
	}

	public void endCameras() throws Exception {
	}

	public void startCamera(float[] position, float[] direction, float[] up,
			float fovy, String name) throws Exception {

	}

	public void endCamera() throws Exception {
	}

	public void startLights() throws Exception {
	}

	public void endLights() throws Exception {
	}

	public void startDirectionalLight(float[] direction, float intensity,
			float[] color, String name) throws Exception {

	}

	public void endDirectionalLight() throws Exception {
	}

	public void startPointLight(float[] position, float intensity,
			float[] color, String name) throws Exception {

	}

	public void endPointLight() throws Exception {
	}

	public void startSpotLight(float[] position, float[] direction,
			float angle, float intensity, float[] color, String name)
			throws Exception {

	}

	public void endSpotLight() throws Exception {
	}

	public void startGeometry() throws Exception {
	}

	public void endGeometry() throws Exception {
	}

	public void startSphere(float radius, String name) throws Exception {

	}

	public void endSphere() throws Exception {
	}

	public void startCylinder(float radius, float height, boolean capped,
			String name) throws Exception {
	}

	public void endCylinder() throws Exception {
	}

	public void startCone(float radius, float height, boolean capped,
			String name) throws Exception {

	}

	public void endCone() throws Exception {
	}

	public void startTorus(float innerRadius, float outerRadius, String name)
			throws Exception {
	}

	public void endTorus() throws Exception {
	}

	public void startTeapot(float size, String name) throws Exception {

	}

	public void endTeapot() throws Exception {
	}

	public void startIndexedTriangleSet(float[][] coordinates,
			float[][] normals, float[][] textureCoordinates,
			int[] coordinateIndices, int[] normalIndices,
			int[] textureCoordinateIndices, String name) throws Exception {
	}

	public void endIndexedTriangleSet() throws Exception {
	}

	public void startTextures() throws Exception {
	}

	public void endTextures() throws Exception {
	}

	public void startTexture(String src, String name) throws Exception {
	}

	public void endTexture() throws Exception {
	}

	public void startMaterials() throws Exception {
	}

	public void endMaterials() throws Exception {
	}

	public void startDiffuseMaterial(float[] color, String name)
			throws Exception {

	}

	public void endDiffuseMaterial() throws Exception {
	}

	public void startPhongMaterial(float[] color, float shininess, String name)
			throws Exception {

	}

	public void endPhongMaterial() throws Exception {
	}

	public void startLinearCombinedMaterial(String material1Name,
			float weight1, String material2Name, float weight2, String name)
			throws Exception {
	}

	public void endLinearCombinedMaterial() throws Exception {
	}

	public void startScene(String cameraName, String[] lightNames,
			float[] background) throws Exception {
	}

	public void endScene() throws Exception {
	}

	public void startShape(String geometryName, String materialName,
			String textureName) throws Exception {
	}

	public void endShape() throws Exception {
	}

	public void startRotate(float[] axis, float angle) throws Exception {
	}

	public void endRotate() throws Exception {
	}

	public void startTranslate(float[] vector) throws Exception {
	}

	public void endTranslate() throws Exception {
	}

	public void startScale(float[] scale) throws Exception {
	}

	public void endScale() throws Exception {
	}

}
